package com.ibm.cloud.banking.media.service;

import com.ibm.cloud.banking.account.dto.AccountInfoDTO;
import com.ibm.cloud.banking.facade.media.dto.MediaCardDTO;
import com.ibm.cloud.banking.media.components.MediaCardBasisComponent;
import com.ibm.cloud.banking.media.convert.MediaCardAccountRelMapper;
import com.ibm.cloud.banking.media.dto.MediaCardAccountRelDTO;
import com.ibm.cloud.banking.media.entity.MediaCardAccountRelEntity;
import com.ibm.cloud.banking.media.repository.MediaCardAccountRelRepository;
import com.ibm.cloud.banking.platform.exception.BusinessException;

import org.apache.camel.Body;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import javax.annotation.Resource;

/**
 * replace desc in here
 *
 * @author HanDong
 * @date 2022/7/18
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
public class MediaCardServiceImpl implements IMediaCardService {

    @Resource
    private MediaCardAccountRelRepository mediaCardAccountRelRepository;

    @Resource
    private MediaCardAccountRelMapper mediaCardAccountRelMapper;

    @Resource
    private MediaCardBasisComponent mediaCardBasisComponent;// mediaCardBasisComponent;


    @Override
    public MediaCardAccountRelDTO getMediaCardAccountRelByCardNo(String cardNo) {
        Optional<MediaCardAccountRelEntity> optionalMediaCardAccountRelEntity = mediaCardAccountRelRepository.findByCardNo(cardNo);
         if(optionalMediaCardAccountRelEntity.isPresent())
         {
             return mediaCardAccountRelMapper.entityToDto(optionalMediaCardAccountRelEntity.get());
         }
         else
         {
             throw new BusinessException("MEDIA-CARD-001","卡/折信息不存在");
         }
    }

    /**
     * TODO 可以只查一个字段增加效率
     * @param cardNo
     * @return
     */
    @Override
    public String getAccountNoByCardNo(String cardNo) {
        return getMediaCardAccountRelByCardNo(cardNo).getAccountNo() ;
    }

    @Override
    public MediaCardDTO allocateMediaCard(@Body AccountInfoDTO accountInfoDTO) {
        MediaCardDTO mediaCardDTO = new MediaCardDTO();
        mediaCardDTO.setCardNo(accountInfoDTO.getMainCardNo());
        mediaCardDTO.setCustomerNo(accountInfoDTO.getCustomerNo());
        // mediaCardDTO.setOpenDate();
        //mediaCardDTO.setTransTellerId(request.getHeader().getTellerId());
        mediaCardDTO.setPassword(accountInfoDTO.getPassword());
        mediaCardDTO.setProductNo(accountInfoDTO.getProductNo());
        mediaCardDTO.setLevel(accountInfoDTO.getCardLevel().toString());
        mediaCardDTO.setType(accountInfoDTO.getAccountCategory());
        mediaCardDTO.setAccountNo(accountInfoDTO.getAccountNo());

        return mediaCardBasisComponent.allocateMediaCard(mediaCardDTO);
    }
}
